package jgcp.worker.process;

import java.util.Iterator;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;

import jgcp.common.Task;
import jgcp.common.TaskStatus;
/**
 * 
 * @Date 31/05/2009
 * @author Peng Zhang(278819) Jie Zhao (288654)
 * @version 1.0
 */
public class TaskQueue {
	private static TaskQueue instance = new TaskQueue();
	private BlockingQueue<Task> queue = new PriorityBlockingQueue<Task>();

	public void put(Task t) throws InterruptedException {
		queue.put(t);
	}

	public Task take() throws InterruptedException {
		return queue.take();
	}

	public void cancelTask(int taskId) {
		for (Iterator<Task> i = queue.iterator(); i.hasNext();) {
			Task t = i.next();
			if (t.getTaskid() == taskId) {
				t.setStatus(TaskStatus.CANCELED);
				queue.remove(t);
			}
		}
	}

	public TaskStatus getStatus(int taskId) {
		for (Iterator<Task> i = queue.iterator(); i.hasNext();) {
			Task t = i.next();
			if (t.getTaskid() == taskId) {
				return t.getStatus();
			}
		}
		return TaskStatus.UNKNOWN;
	}

	public static TaskQueue getInstance() {
		return instance;
	}
}
